using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Publicuse.Appication;
using Publicuse.Application;
using Publicuse.Autofac;
using Publicuse.Entity;
using Publicuse.Export;
using Publicuse.Interface;
using Publicuse.Interface.Extend;
using Geekeradmin.Authorize;

namespace Geekeradmin.Controllers
{
  /// <summary>
  /// 消息通知管理模块
  /// </summary>
  [UserAuthorize("Admin")]
  public class NoticeanController : BaseController
  {
    public NoticeanController(
      IAutofaceInstance autoface,
      IMySqlSugarService mySqlSugar,
      IWebHostEnvironment webHost,
      IOptions<DBConnectionOption> options,
      IBaseServiceExt baseServiceExt
    )
      : base(autoface, mySqlSugar, webHost, options, baseServiceExt) { }

    /// <summary>
    /// 获取消息通知管理信息列表
    /// </summary>
    /// <param name="query"></param>
    /// <returns></returns>
    ///
    [HttpPost("GetList"), AllowAnonymous]
    public async Task<JsonResult> GetList(Noticean query)
    {
      CommonResult result = await NoticeanApplication.GetApplication(container).GetList(query);
      return new JsonResult(result);
    }

    /// <summary>
    /// 获取消息通知管理详细信息
    /// </summary>
    /// <param name="Id"></param>
    /// <returns></returns>
    [HttpGet("GetListb")]
    public async Task<JsonResult> GetDetail(int Id)
    {
      CommonResult result = await NoticeanApplication.GetApplication(this.container).GetDetail(Id);
      return new JsonResult(result);
    }

    /// <summary>
    /// 获取当前用户的消息通知管理信息列表
    /// </summary>
    /// <returns></returns>
    ///
    [HttpGet("GetListc"), AllowAnonymous]
    public async Task<JsonResult> GetListc()
    {
      CommonResult result = await NoticeanApplication.GetApplication(container).GetListc();
      return new JsonResult(result);
    }

    /// <summary>
    /// 获取当前用户未读的消息通知管理信息列表
    /// </summary>
    /// <returns></returns>
    ///
    [HttpGet("GetListd"), AllowAnonymous]
    public async Task<JsonResult> GetListd()
    {
      CommonResult result = await NoticeanApplication.GetApplication(container).GetListd();
      return new JsonResult(result);
    }

    /// <summary>
    /// 赋值已读用户
    /// </summary>
    /// <param name="title"></param>
    /// <param name="type"></param>
    /// <param name="Id"></param>
    /// <returns></returns>
    [HttpGet("readuser"), AllowAnonymous]
    public async Task<JsonResult> readuser(string title, string type, string Id = "")
    {
      CommonResult result = await NoticeanApplication
        .GetApplication(container)
        .readuser(title, type, Id);
      return new JsonResult(result);
    }

    /// <summary>
    /// 检查数据是否存在
    /// </summary>
    /// <param name="title"></param>
    /// <param name="type"></param>
    /// <param name="Id"></param>
    /// <returns></returns>
    [HttpGet("CheckData"), AllowAnonymous]
    public async Task<JsonResult> CheckData(string title, string type, string Id = "")
    {
      CommonResult result = await NoticeanApplication
        .GetApplication(container)
        .CheckData(title, type, Id);
      return new JsonResult(result);
    }

    /// <summary>
    /// 获取已读信息
    /// </summary>
    /// <returns></returns>
    [HttpGet("GetRead"), AllowAnonymous]
    public async Task<JsonResult> GetRead()
    {
      CommonResult result = await NoticeanApplication.GetApplication(container).GetRead();
      return new JsonResult(result);
    }

    /// <summary>
    /// 新增修改消息通知管理信息
    /// </summary>
    /// <param name="info"></param>
    /// <returns></returns>
    [HttpPost("AddOrUpdate")]
    public async Task<JsonResult> AddOrUpdate(Noticean info)
    {
      info.readornot = string.Empty;
      CommonResult result = await NoticeanApplication.GetApplication(container).AddOrUpdate(info);
      return new JsonResult(result);
    }

    /// <summary>
    /// 消息通知管理数据导入
    /// </summary>
    /// <returns></returns>
    [HttpPost("ImportExcel")]
    public async Task<JsonResult> ImportExcel()
    {
      string path = webHost.WebRootPath;
      var files = Request.Form.Files;
      string loginUserCode = Request.Form["loginUserCode"];
      CommonResult result = await NoticeanApplication
        .GetApplication(container)
        .ImportExcel(files, path, loginUserCode);
      return new JsonResult(result);
    }

    /// <summary>
    /// 批量审核数据
    /// </summary>
    /// <param name="data"></param>
    /// <param name="loginUserCode"></param>
    /// <returns></returns>
    [HttpGet("ConfirmAll"), AllowAnonymous]
    public async Task<JsonResult> ConfirmAll(string data, string loginUserCode)
    {
      CommonResult result = await NoticeanApplication
        .GetApplication(container)
        .ConfirmAll(data, loginUserCode);
      return new JsonResult(result);
    }

    /// <summary>
    /// 审核数据
    /// </summary>
    /// <param name="id"></param>
    /// <param name="status"></param>
    /// <param name="loginUserCode"></param>
    /// <returns></returns>
    [HttpGet("Confirm"), AllowAnonymous]
    public async Task<JsonResult> Confirm(string id, int status, string loginUserCode)
    {
      CommonResult result = await NoticeanApplication
        .GetApplication(container)
        .Confirm(id, status, loginUserCode);
      return new JsonResult(result);
    }

    /// <summary>
    /// 删除消息通知管理信息
    /// </summary>
    /// <param name="Id"></param>
    /// <param name="loginUserCode"></param>
    /// <param name="type"></param>
    /// <returns></returns>
    [HttpGet("Delete")]
    public async Task<JsonResult> Delete(string Id, string loginUserCode, string type = "-9")
    {
      CommonResult result = await NoticeanApplication
        .GetApplication(container)
        .Delete(Id, loginUserCode, type);
      return new JsonResult(result);
    }

    /// <summary>
    /// 还原消息通知管理删除信息
    /// </summary>
    /// <param name="Id"></param>
    /// <param name="loginUserCode"></param>
    /// <returns></returns>
    [HttpGet("Reduction"), AllowAnonymous]
    public async Task<JsonResult> Reduction(string Id, string loginUserCode)
    {
      CommonResult result = await NoticeanApplication
        .GetApplication(container)
        .Reduction(Id, loginUserCode);
      return new JsonResult(result);
    }

    /// <summary>
    /// 导出数据信息
    /// </summary>
    /// <param name="query"></param>
    /// <returns></returns>
    [HttpGet("ExportData")]
    public async Task<IActionResult> ExportData([FromQuery] Noticean query)
    {
      return await InitExportData(
        webHost,
        mySqlSugar,
        query,
        "\\template\\report\\noticean.xls",
        "消息通知管理统计表",
        (a, b, c) =>
        {
          var ms = NoticeanExport.ExportData(a, b, c, out var msgb);
          return ms;
        },
        null,
        "",
        "",
        "",
        query.pageSize
      );
    }
  }
}
